//
//  BubbleSort.swift
//  Sorts
//
//  Created by lhc on 2023/12/11.
//

import Foundation

class BubbleSort: BaseSort {
    
    override func sort(action: ((OperatorType, (Int, Int)) -> ())) {
        for i in 0..<array.count {
            
            var count = 0
            for j in 0..<array.count - i - 1 {
                if array[j] > array[j + 1] {
                    
                    action(.exchange, (j, j + 1))
                    exchange(a: j, b: j + 1)
                    count = 1
                }
            }
            if count == 0 {
                break
            }
        }
    }
}
